package com.web.controller;

import com.common.utils.Page;
import com.entity.*;
import com.service.InformationService;
import com.service.InformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 用户控制器类
 */
@Controller
public class InformationController {
	// 依赖注入
	@Autowired
	private InformationService informationService;
	/**
	 * 分页查询
	 * 
	 */
	@RequestMapping(value = "/information/list.action")
	public String list(@RequestParam(defaultValue="1")Integer page,
			@RequestParam(defaultValue="10")Integer rows,String title, 
			Model model,HttpServletRequest request,HttpSession session) {
		// 条件查询所有
		title=request.getParameter("title");
		if(title==null) title="";
		String title1=request.getParameter("title1");
		if(title1==null) title1="0";
		if(title1.equals("1")) {
			String str=(String) session.getAttribute("title");
			if(str!=null&&str!="") title=str;
		}
		Page<Information> informations = informationService
				.findInformationList(page, rows,title);
		model.addAttribute("page", informations);
		session.setAttribute("title", title);
		return "admin/information";
	}
	@RequestMapping(value = "/user/information/list.action")
	public String list1(@RequestParam(defaultValue="1")Integer page,
			@RequestParam(defaultValue="10")Integer rows,String title, Model model,
			HttpServletRequest request,HttpSession session) {
		// 条件查询所有
		title=request.getParameter("title");
		if(title==null) title="";
		String title1=request.getParameter("title1");
		if(title1==null) title1="0";
		if(title1.equals("1")) {
			String str=(String) session.getAttribute("title");
			if(str!=null&&str!="") title=str;
		}
		Page<Information> informations = informationService
				.findInformationList(page, rows,title);
		model.addAttribute("page", informations);
		session.setAttribute("title", title);
		model.addAttribute("pages", page);
		return "user/information";
	}
	/**
	 * 通过id获取信息
	 */
	@RequestMapping("/information/getInformationById.action")
	@ResponseBody
	public Information getInformationById(Integer id) {
	    Information information = informationService.getInformationById(id);
	    return information;
	}
	@RequestMapping(value ="/user/information/getInformationById.action", method = RequestMethod.GET)
	public String getInformation(@RequestParam(defaultValue="0")Integer id,Model model,
			@RequestParam(defaultValue="1")Integer pages,HttpSession session) {
	    Information information = informationService.getInformationById(id);
		model.addAttribute("information", information);
		session.setAttribute("information_art", information.getArticle());
		model.addAttribute("pages",pages);
		model.addAttribute("id",id);
	    return "user/c_information";
	}
	
	/**
	 * 创建
	 */
	@RequestMapping("/information/create.action")
	@ResponseBody
	public String informationCreate(Information information,HttpSession session) {
		if(information.getTitle().equals("")||information.getTitle().equals(null)) {
			return "title";
		}
		if(information.getArticle().equals("")||information.getArticle().equals(null)) {
			return "article";
		}
		Admin username=(Admin)session.getAttribute("Admin_SESSION");
		information.setUsername(username.getAdmin_name());
		Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(d);
		information.setTime(today);
		System.out.println(today+","+username.getAdmin_name());
		String article="";
		article=information.getArticle().replaceAll("\r\n", "");
		information.setArticle(article);
	    // 执行Service层中的创建方法，返回的是受影响的行数
	    int rows = informationService.createInformation(information);
	    if(rows > 0){
	        return "OK";
	    }else{
	        return "FAIL";
	    }
	}
	/**
	 * 更新
	 */
	@RequestMapping("/information/updateInformation.action")
	@ResponseBody
	public String informationUpdate(Information information) {
		System.out.println(information.getTime());
		if(information.getTitle().equals("")||information.getTitle().equals(null)) {
			System.out.println(information.getTitle());
			return "title";
		}
		if(information.getArticle().equals("")||information.getArticle().equals(null)) {
			System.out.println(information.getTitle());
			return "article";
		}
		String article="";
		article=information.getArticle().replaceAll("\r\n", "");
		information.setArticle(article);
		int rows = informationService.updateInformation(information);
		if(rows > 0){
		    return "OK";
		}else{
		    return "FAIL";
		}
	}
	/**
	 * 删除
	 */
	@RequestMapping("/information/delete.action")
	@ResponseBody
	public String informationDelete(Integer id) {
	    int rows = informationService.deleteInformation(id);
	    if(rows > 0){			
	        return "OK";
	    }else{
	        return "FAIL";			
	    }
	}

}
